package cn.com.sise.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @Author leiHua
 * @Date 2022/11/11 18:36
 */
@WebServlet("/downLoadServlet")
public class DownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream;charset=UTF-8");
        String filename = request.getParameter("pic");
        System.out.println(filename);
        response.addHeader("Content-Disposition","attachment;filename="+filename);
        FileInputStream fileInputStream = new FileInputStream("F:\\code\\java\\javaweb\\javaee\\lab10\\web\\uploadFile\\" + filename);
        ServletOutputStream outputStream = response.getOutputStream();
        byte[] bytes=new byte[128];
        int len=0;
        while ((len= fileInputStream.read(bytes))!=-1){
            outputStream.write(bytes,0,len);
        }
        fileInputStream.close();
        outputStream.flush();
        outputStream.close();

    }
}
